﻿using CPF;
using CPF.Animation;
using CPF.Charts;
using CPF.Controls;
using CPF.Drawing;
using CPF.Shapes;
using CPF.Styling;
using CPF.Svg;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace CPFUI.IControl
{
    [CPF.Design.DesignerLoadStyle("res://$safeprojectname$/Stylesheet1.css")]//用于设计的时候加载样式
    public class OfficeHeader : Control
    {
        Panel tabHeader;
        //模板定义
        protected override void InitializeComponent()
        {
            if (DesignMode)
            {
                MinHeight = 20;
                MinWidth = 20;
            }
            tabHeader = new StackPanel()
            {
                Orientation = Orientation.Horizontal,
                MarginLeft = 0,
                MarginRight = 0,
                Height = "100%",
                MarginTop = 0,
                ClipToBounds = true,
                Children =
                {
                }
            };
            Children.Add(tabHeader);
        }

        public void ClearTabHeader()
        {
            tabHeader.Children.Clear();
            //tabHeader.Children.Add(new Panel()
            //{
            //    Width = PaddingLeft,
            //    Height = "100%",
            //    Bindings = {
            //        { 
            //            nameof(Panel.Width),
            //            nameof(PaddingLeft),
            //            this
            //        }
            //    }
            //});
        }
        public Panel AddTabHeader(string name, bool isSelected)
        {
            Panel bar = new Panel
            {
                Name = "tabItemPanel",
                Cursor =Cursors.Hand,
                IsGroup = isSelected,
                IsAntiAlias = true,
                MarginTop = 2,
                MarginBottom = 0,
                Children =
                {
                        new Panel {

                            Name="tabItemSpacer",
                            Width="100%",
                            Height=4,
                            MarginLeft=0,
                            MarginBottom=-2,
                            MarginRight=0,
                        },
                         new TextBlock {

                            Name="tabItemText",
                             MarginLeft=20,
                             MarginRight=20,
                             Text = name }
                    }

            };
            bar.MouseDown += Bar_MouseDown;
            tabHeader.Children.Add(bar);
            return bar;
        }
        private void Bar_MouseDown(object sender, CPF.Input.MouseButtonEventArgs e)
        {
            Panel pnl = (Panel)sender;
            TabSlectedChanged(pnl, true);

            for (int i = 0; i < tabHeader.Children.Count; i++)
            {
                if (tabHeader.Children[i] != sender)
                {
                    TabSlectedChanged((Panel)tabHeader.Children[i], false);
                }
            }
            TextBlock txt = pnl.Children[1] as TextBlock;
            TabHeaderClick(new OfficeHeaderEvenArgs(txt.Text));
        }
        public void TabSlectedChanged(Panel tab, bool isSelected)
        {
            tab.IsGroup = isSelected;
        }
        public void TabHeaderClick(OfficeHeaderEvenArgs e)
        {
            OnTabHeaderClick?.Invoke(this, e);
        }
        public event EventHandler<OfficeHeaderEvenArgs> OnTabHeaderClick;
#if !DesignMode //用户代码写到这里，设计器下不执行，防止设计器出错
        protected override void OnInitialized()
        {
            base.OnInitialized();

        }
        //用户代码

#endif
    }
}
